const Sqlite3Util = appRequire('modules/Sqlite3Util');
const PageResultVo = appRequire('vo/PageResultVo.js');
class SysUserSqlite3Dao {
  constructor() {

  }
  static async create() {
    let db = await Sqlite3Util.connect(appConfig.dbPath);
    await Sqlite3Util.run(db, `CREATE TABLE "sys_user" (
            "id" VARCHAR(64) NOT NULL,
              "nick_name" VARCHAR(64) DEFAULT '',
            "username" VARCHAR(64) DEFAULT '',
            "password" VARCHAR(64) DEFAULT '',
              "user_icon" VARCHAR(255) DEFAULT '',
              "short_phone_num" VARCHAR(64) DEFAULT '',
              "phone_num" VARCHAR(32) DEFAULT '',
              "email" VARCHAR(255) DEFAULT '',
            "remarks" VARCHAR(255),
            "create_time" VARCHAR(255),
            "update_time" VARCHAR(255),
              "is_used" VARCHAR(10) DEFAULT '0',
            "is_hide" VARCHAR(10) DEFAULT '0',
            "sort" integer,
            PRIMARY KEY ("id")
          );`);
    await Sqlite3Util.close(db);
  }
  static async save(user) {
    let db = await Sqlite3Util.connect(appConfig.dbPath);
    try {
      await Sqlite3Util.run(db, `insert into sys_user
          (id,nick_name,username,password,phone_num,create_time,update_time) 
          values(?,?,?,?,?,?,?)`,
        [user.id, user.nickName, user.username, user.password, user.phoneNum, user.createTime, user.updateTime]);
    } catch (error) {
      throw new Error(error);
    } finally {
      await Sqlite3Util.close(db);
    }
  }
  static async update(user) {
    let db = await Sqlite3Util.connect(appConfig.dbPath);
    try {
      await Sqlite3Util.run(db, `update sys_user 
        set nick_name=?,password=?,user_icon=?,phone_num=?,short_phone_num=?,email=?,remarks=?,update_time=?,is_used=?,is_hide=?,sort=?`,
      [user.nickName, user.password, user.userIcon, user.phoneNum, user.shortPhoneNum, user.email, user.remarks, user.updateTime, user.isUsed, user.isHide, user.sort]);
    } catch (error) {
      throw new Error(error);
    } finally {
      await Sqlite3Util.close(db);
    }
  }
  static async deleteById(user) {
    let db = await Sqlite3Util.connect(appConfig.dbPath);
    try {
      await Sqlite3Util.run(db, `delete from sys_user where id = ?`,
      [user.id]);
    } catch (error) {
      throw new Error(error);
    } finally {
      await Sqlite3Util.close(db);
    }
  }
  static async findAll() {
    let db = await Sqlite3Util.connect(appConfig.dbPath);
    try {
      let data = await Sqlite3Util.each(db, `select * from sys_user`);
      return data;
    } catch (error) {
      throw new Error(error);
    } finally {
      await Sqlite3Util.close(db);
    }
  }
  static async findByPage(page) {
    let db = await Sqlite3Util.connect(appConfig.dbPath);
    try {
      let result = await Sqlite3Util.get(db,`select count(*) as totalNum from sys_user`);
      console.log(result);
      let count = result.totalNum;
      let data = await Sqlite3Util.each(db, `select * from sys_user limit ?,?`,
      [(page.pageNum-1)*page.pageSize,page.pageSize]);
      return PageResultVo.success('success',count,data);
    } catch (error) {
      throw new Error(error);
    } finally {
      await Sqlite3Util.close(db);
    }   
  }
}
// //数据库的名是"mydatebase.db"
// var sqlite3 = require("sqlite3");

// //创建数据库
// var database = new sqlite3.Database("mydatebase.db", function (err) {
//     if (err) {
//         console.log("new database error,", err.message);
//     } else {
//         console.log("new database success");

//         //创建表
//         database.run("create table if not exists sharewaf_data(time NUMERIC, domain TEXT, ip TEXT, lon_lat TEXT, address TEXT, url TEXT, type TEXT, agent TEXT)", function (err) {
//             if (err) {
//                 console.log("create database error,", err.message);
//             } else {
//                 console.log("create database success");

//                 //插入数据
//                 database.run("insert into sharewaf_data(time, domain, ip, lon_lat, address, url, type, agent) VALUES(?,?,?,?,?,?,?,?)", ["time", "domain", "ip", "lon_lat", "taiyuan", "url", "type", "agent"], function (err) {
//                     if (err) {
//                         console.log("insert data error,", err.message);
//                     } else {
//                         console.log("insert data success");

//                         //查询
//                         database.all("select * from sharewaf_data", function (err, rows) {
//                             if (err) {
//                                 console.log("select from sharewaf_data error,", err.message);
//                             } else {
//                                 console.log(rows);
//                             }
//                         });
//                     }

//                 });
//             }
//         });
//     }
// });
module.exports = SysUserSqlite3Dao;